package com.lill.workflow.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lill.common.component.transaction.CommonTransaction;
import com.lill.common.component.transaction.TM;
import com.lill.common.component.util.IdUtil;
import com.lill.workflow.constant.AuditType;
import com.lill.workflow.dao.IWorkflowCurrentDao;
import com.lill.workflow.dao.IWorkflowCurrentHistoryDao;
import com.lill.workflow.dao.IWorkflowInfoDao;
import com.lill.workflow.dao.IWorkflowStepHistoryDao;
import com.lill.workflow.model.WorkflowCurrentHistoryModel;
import com.lill.workflow.model.WorkflowCurrentModel;
import com.lill.workflow.model.WorkflowEmployeeModel;
import com.lill.workflow.model.WorkflowInfoModel;
import com.lill.workflow.model.WorkflowStepHistoryModel;
import com.lill.workflow.service.IWorkflowCurrentService;

/**
 * Service实现类
 *
 * @author lill
 * @date 2019-05-26 12:05:20
 */
@Service("workflowCurrentService")
public class WorkflowCurrentServiceImpl implements IWorkflowCurrentService {
    @Autowired
    private IWorkflowCurrentDao workflowCurrentDao;
    @Autowired
    private IWorkflowCurrentHistoryDao workflowCurrentHistoryDao;
    @Autowired
    private IWorkflowStepHistoryDao stepHistoryDao;
    @Autowired
    private IWorkflowInfoDao workflowInfoDao;

    public WorkflowCurrentModel queryObject(String id) {
    	Map<String, Object> map=new HashMap<String, Object>();
    	map.put("id", id);
    	WorkflowCurrentModel currentWorkflow=workflowCurrentDao.queryObject(id);
    	currentWorkflow.setHistoryList(stepHistoryDao.queryList(map));
        return currentWorkflow;
    }

	public WorkflowCurrentModel queryCurrent(Map<String, Object> params) {
		return  workflowCurrentDao.queryCurrent(params);
	}

    public List<WorkflowCurrentModel> queryList(Map<String, Object> map) {
        return workflowCurrentDao.queryList(map);
    }
    public WorkflowCurrentModel queryBusiness(String businessId) {
        return workflowCurrentDao.queryBusiness(businessId);
    }

    public int queryTotal(Map<String, Object> map) {
        return workflowCurrentDao.queryTotal(map);
    }

    @CommonTransaction({TM.flow})
    public WorkflowCurrentModel saveBean(WorkflowCurrentModel workflowCurrent) {
    	WorkflowCurrentModel OldCurrentModel=workflowCurrentDao.queryBusiness(workflowCurrent.getBusinessId());
    	if(null!=OldCurrentModel){
    		WorkflowCurrentHistoryModel currentHistoryModel=new WorkflowCurrentHistoryModel(OldCurrentModel);
    		workflowCurrentHistoryDao.save(currentHistoryModel);
    		workflowCurrentDao.delete(OldCurrentModel.getId());
    		
    		WorkflowInfoModel workflowInfoVo=workflowInfoDao.queryObject(OldCurrentModel.getWorkflowId());
    		if(null!=workflowInfoVo&&1!=workflowInfoVo.getStatus()){
    			throw new RuntimeException("此流程冻结，无法使用！");
    		}
			Map<String, Object> stringIntegerMap = workflowInfoDao.queryWorkflowInvalidNum(OldCurrentModel.getWorkflowId());
			if(Integer.parseInt(stringIntegerMap.get("num").toString())>0){
    			throw new RuntimeException("此流程有员工无效无法继续，无法使用！");
    		}
    	}
    	
    	WorkflowEmployeeModel stepEmployee=workflowInfoDao.queryFirstStep(workflowCurrent.getWorkflowId());
    	workflowCurrent.setCurrentEmployeeId(stepEmployee.getId());
    	workflowCurrent.setNextEmployeeId(stepEmployee.getNextId());
    	workflowCurrent.setAudit(AuditType.ING.getVal());
    	int result=workflowCurrentDao.save(workflowCurrent);
    	if(result>0){
    		return workflowCurrent;
    	}else{
    		return null;
    	}
    }
    
    @CommonTransaction({TM.flow})
    public int save(WorkflowCurrentModel workflowCurrent) {
    	WorkflowCurrentModel OldCurrentModel=workflowCurrentDao.queryBusiness(workflowCurrent.getBusinessId());
    	if(null!=OldCurrentModel){
    		WorkflowCurrentHistoryModel currentHistoryModel=new WorkflowCurrentHistoryModel(OldCurrentModel);
    		workflowCurrentHistoryDao.save(currentHistoryModel);
    		workflowCurrentDao.delete(OldCurrentModel.getId());
    		
    		WorkflowInfoModel workflowInfoVo=workflowInfoDao.queryObject(OldCurrentModel.getWorkflowId());
    		if(null!=workflowInfoVo&&1==workflowInfoVo.getStatus()){
    			throw new RuntimeException("此流程冻结，无法使用！");
    		}
			Map<String, Object> stringIntegerMap = workflowInfoDao.queryWorkflowInvalidNum(OldCurrentModel.getWorkflowId());
			if(Integer.parseInt(stringIntegerMap.get("num").toString())>0){
    			throw new RuntimeException("此流程有员工无效无法继续，无法使用！");
    		}
    	}
    	
        WorkflowEmployeeModel stepEmployee=workflowInfoDao.queryFirstStep(workflowCurrent.getWorkflowId());
        workflowCurrent.setCurrentEmployeeId(stepEmployee.getId());
        workflowCurrent.setNextEmployeeId(stepEmployee.getNextId());
        workflowCurrent.setAudit(AuditType.ING.getVal());
        return workflowCurrentDao.save(workflowCurrent);
    }
    
    @CommonTransaction({TM.flow})
	public WorkflowCurrentModel saveWithOriginator(WorkflowCurrentModel workflowCurrent,String Originator) {
    	WorkflowCurrentModel OldCurrentModel=workflowCurrentDao.queryBusiness(workflowCurrent.getBusinessId());
    	if(null!=OldCurrentModel){
    		WorkflowCurrentHistoryModel currentHistoryModel=new WorkflowCurrentHistoryModel(OldCurrentModel);
    		workflowCurrentHistoryDao.save(currentHistoryModel);
    		workflowCurrentDao.delete(OldCurrentModel.getId());
    		
    		WorkflowInfoModel workflowInfoVo=workflowInfoDao.queryObject(OldCurrentModel.getWorkflowId());
    		if(null!=workflowInfoVo&&1==workflowInfoVo.getStatus()){
    			throw new RuntimeException("此流程冻结，无法使用！");
    		}
			Map<String, Object> stringIntegerMap = workflowInfoDao.queryWorkflowInvalidNum(OldCurrentModel.getWorkflowId());
			if(Integer.parseInt(stringIntegerMap.get("num").toString())>0){
    			throw new RuntimeException("此流程有员工无效无法继续，无法使用！");
    		}
    	}
    	
		if(StringUtils.isEmpty(workflowCurrent.getId())){
			workflowCurrent.setId(IdUtil.createIdbyUUID());
		}
		
		WorkflowEmployeeModel stepEmployee=workflowInfoDao.queryFirstStep(workflowCurrent.getWorkflowId());
    	workflowCurrent.setCurrentEmployeeId(stepEmployee.getId());
    	workflowCurrent.setNextEmployeeId(stepEmployee.getNextId());
    	workflowCurrent.setAudit(AuditType.ING.getVal());
    	workflowCurrentDao.save(workflowCurrent);
		
		WorkflowStepHistoryModel workflowStepHistoryModel = new WorkflowStepHistoryModel();
        workflowStepHistoryModel.setId(IdUtil.createIdbyUUID());
        workflowStepHistoryModel.setCurrentWorkflowId(workflowCurrent.getId());
        workflowStepHistoryModel.setAudit(1);
        workflowStepHistoryModel.setAuditDate(new Date());
        workflowStepHistoryModel.setEmployeeId(Originator);
        stepHistoryDao.save(workflowStepHistoryModel);
		return workflowCurrent;
	}


	public WorkflowCurrentModel updateAuty(WorkflowCurrentModel workflowCurrent){
		WorkflowCurrentModel beforeWorkflow=workflowCurrentDao.queryBusiness(workflowCurrent.getBusinessId());
		beforeWorkflow.setAudit(workflowCurrent.getAudit());
		beforeWorkflow.setReason(workflowCurrent.getReason());
		beforeWorkflow.setCurrentEmployeeId(workflowCurrent.getCurrentEmployeeId());
		return beforeWorkflow;
	}

	public int updateAutyAudit(WorkflowCurrentModel workflowCurrent){
		WorkflowCurrentModel current=updateAuty(workflowCurrent);
		return update(current);
	}

    @Transactional
    public int update(WorkflowCurrentModel workflowCurrent) {
    	WorkflowCurrentModel beforeWorkflow=workflowCurrentDao.queryObject(workflowCurrent.getId());
    	WorkflowStepHistoryModel stepHistory=new WorkflowStepHistoryModel();
    	stepHistory.setId(IdUtil.createIdbyUUID());
    	stepHistory.setCurrentWorkflowId(workflowCurrent.getId());
    	stepHistory.setEmployeeId(beforeWorkflow.getCurrentEmployeeId());
    	stepHistory.setAudit(workflowCurrent.getAudit());
    	stepHistory.setAuditDate(new Date());
    	stepHistory.setContent(workflowCurrent.getReason());
    	stepHistoryDao.save(stepHistory);
    	
    	if(AuditType.YES.getVal()==workflowCurrent.getAudit()&&StringUtils.isNotEmpty(beforeWorkflow.getNextEmployeeId())){
    		Map<String, Object> mapNext=new HashMap<String, Object>();
    		mapNext.put("workflowId", workflowCurrent.getWorkflowId());
    		mapNext.put("currentEmployeeId", beforeWorkflow.getNextEmployeeId());
    		mapNext.put("ordernum", (beforeWorkflow.getStep()==null?0:beforeWorkflow.getStep())+1);
    		
    		WorkflowEmployeeModel stepEmployee=workflowInfoDao.queryStepNext(mapNext);
    		if(null!=stepEmployee){
    			if(2==stepEmployee.getState()){
    				workflowCurrent.setAudit(AuditType.ING.getVal());
        			
        			workflowCurrent.setCurrentEmployeeId(stepEmployee.getId());
        			workflowCurrent.setNextEmployeeId(stepEmployee.getNextId());
//        			workflowCurrent.setStep(stepNum);
    			}else{
    				workflowCurrent.setAudit(AuditType.NO.getVal());
    				workflowCurrent.setReason("查看员工状态，联系公司管理员！");
        			
        			workflowCurrent.setCurrentEmployeeId(beforeWorkflow.getNextEmployeeId());
        			workflowCurrent.setNextEmployeeId(null);
        			
        			WorkflowStepHistoryModel stepNextHistory=new WorkflowStepHistoryModel();
        			stepNextHistory.setId(IdUtil.createIdbyUUID());
        			stepNextHistory.setCurrentWorkflowId(workflowCurrent.getId());
        			stepNextHistory.setEmployeeId(beforeWorkflow.getNextEmployeeId());
        			stepNextHistory.setAudit(workflowCurrent.getAudit());
        			stepNextHistory.setAuditDate(new Date());
        			stepNextHistory.setContent(workflowCurrent.getReason());
        	    	stepHistoryDao.save(stepNextHistory);
    			}
    		}else{
    			workflowCurrent.setCurrentEmployeeId(beforeWorkflow.getNextEmployeeId());
    			workflowCurrent.setNextEmployeeId(null);
//    			workflowCurrent.setStep(stepNum);
    		}
    	}
    	
        return workflowCurrentDao.update(workflowCurrent);
    }


	@Override
	public int undo(WorkflowCurrentModel workflowCurrent) {
		return workflowCurrentDao.update(workflowCurrent);
	}

	@Override
	public List<WorkflowStepHistoryModel> queryObjectOne(Map<String, Object> hashMap) {
		return workflowCurrentDao.queryObjectOne(hashMap);
	}



	@Override
	public WorkflowCurrentModel listQuite(Map<String, Object> hashMap) {
		return workflowCurrentDao.listQuite(hashMap);
	}

	@Override
	public WorkflowCurrentModel listFormal(Map<String, Object> hashMap) {
		return workflowCurrentDao.listFormal(hashMap);
	}

	@Override
	public List<WorkflowStepHistoryModel> queryNextOne(Map<String, Object> hashMap) {
		return workflowCurrentDao.queryNextOne(hashMap);
	}

	@Override
	public List<WorkflowStepHistoryModel> queryNextOneFormal(Map<String, Object> hashMap) {
		return workflowCurrentDao.queryNextOneFormal(hashMap);
	}

	@Override
	public List<WorkflowStepHistoryModel> queryObjectOneFormal(Map<String, Object> hashMap) {
		return workflowCurrentDao.queryObjectOneFormal(hashMap);
	}
	
	@Override
	public WorkflowCurrentModel listRenew(Map<String, Object> hashMap) {
		return workflowCurrentDao.listRenew(hashMap);
	}

	@Override
	public int queryCopyTotal(Map<String, Object> queryData) {
		return workflowCurrentDao.queryCopyTotal(queryData);
	}

	@Override
	public List<WorkflowStepHistoryModel> queryNextOneRenew(Map<String, Object> hashMap) {
		return workflowCurrentDao.queryNextOneRenew(hashMap);
	}

}
